package com.example.demo.ocr;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @author calwen
 * @since 2023/10/13
 */
@RestController
@RequestMapping("/ocr")
public class OcrController {
    @Resource
    Tesseract tesseract;

    @PostMapping(value = "/recognize")
    public String recognizeImage(@RequestParam MultipartFile file) throws IOException, TesseractException {
        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
        return tesseract.doOCR(bufferedImage);
    }

}
